using System;
using UnityEngine;
using UnityEngine.UI;

public class LoopManager : MonoBehaviour
{
    public static LoopManager Instance { get; private set; }

    [Header("UI显示")]
    public Text loopText; // UnityEngine.UI.Text
    // 如需支持TMP，可自行扩展

    public int CurrentLoopCount { get; private set; } = 0;

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
    }

    private void Start()
    {
        UpdateLoopUI();
    }

    /// <summary>
    /// 增加一次循环（如人物走完一圈后调用）
    /// </summary>
    public void AddLoop()
    {
        CurrentLoopCount++;
        UpdateLoopUI();
        EventHandler.CallLoopCompletedEvent(CurrentLoopCount);
    }

    /// <summary>
    /// 重置循环次数
    /// </summary>
    public void ResetLoop()
    {
        CurrentLoopCount = 0;
        UpdateLoopUI();
    }

    /// <summary>
    /// 更新UI显示
    /// </summary>
    private void UpdateLoopUI()
    {
        if (loopText != null)
            loopText.text = $"循环次数：{CurrentLoopCount}";
    }

    public void RefreshUI() => UpdateLoopUI();

    private void OnEnable()
    {
        EventHandler.LoopCompletedEvent += OnLoopCompletedEvent;
    }

    private void OnDisable()
    {
        EventHandler.LoopCompletedEvent -= OnLoopCompletedEvent;
    }

    // 示例：循环完成时刷新地图、怪物等
    private void OnLoopCompletedEvent(int loopCount)
    {
        Debug.Log($"第{loopCount}次循环，地图可刷新怪物、事件等");
        // TODO: 在这里调用地图刷新、怪物刷新等方法
    }
} 